<?php


namespace App\Http\Repositories\StoreCenter;


use App\Exceptions\ApiException;
use App\Http\Repositories\BaseRepository;
use App\Models\StoreCenter\Models\CouponModel;

class CouponRepository extends BaseRepository
{
    public $coupon;
    public function __construct(CouponModel $coupon)
    {
        $this->coupon = $coupon;
    }

    /**
     * 处理保存数据
     * @param $data
     * @return mixed
     * @throws ApiException
     */
    public function handelSaveData($data)
    {
        switch ($data['condition']) {
            //无条件 重置条件
            case $this->coupon::NOT_CONDITION:
                $data['least_fee'] = 0.00;
                $data['purchase_num'] = 0;
                $data['discount_fee'] = 0.00;
                break;
            case $this->coupon::NUMERIC_CONDITION:
                if ($data['discount_fee'] >= $data['least_fee'])
                    $this->throwError([0, '折扣不能大于总花费']);
                break;
            case $this->coupon::PERCENT_CONDITION:
                if ($data['discount_fee'] >= 1)
                    $this->throwError([0, '折扣比例不能大于100%']);
            default:
                break;
        }
        return $data;
    }

    /**
     * 添加优惠券
     * @param $data
     * @return mixed
     * @throws ApiException
     */
    public function addCoupon($data)
    {
        $data = $this->handelSaveData($data);

        return $this->coupon->addItem($data, true);
    }

    /**
     * 删除优惠券
     * @param $data
     * @return array|string[]
     */
    public function delCoupon($data)
    {
        return $this->coupon->deleteItem($data);
    }

    /**
     * 编辑优惠券
     * @param $data
     * @return array
     * @throws ApiException
     */
    public function editCoupon($data)
    {
        is_real_exists(@$data['condition']) && $data = $this->handelSaveData($data);

        $where['id'] = $data['id'];
        return $this->coupon->editItem($where, $data);
    }

    /**
     * 获取优惠券详情
     * @param $data
     * @return mixed
     * @throws ApiException
     */
    public function getCouponInfo($data)
    {
        if (isset($data['id']))
            $where['id'] = $data['id'];
        else
            $where['code'] = $data['code'];

        return $this->coupon->getInfo($where);
    }

    /**
     * 获取优惠券列表
     * @param $data
     * @return mixed
     */
    public function getCouponList($data)
    {
        $where = [];
        is_real_exists(@$data['code']) && $where['code'] = $data['code'];
        is_real_exists(@$data['store_id']) && $where['store_id'] = $data['store_id'];
        is_real_exists(@$data['condition']) && $where['condition'] = $data['condition'];
        is_real_exists(@$data['create_user_uuid']) && $where['create_user_uuid'] =  $data['create_user_uuid'];
        is_real_exists(@$data['start_time']) && $where['start_time'] = ['>=', $data['start_time']];
        is_real_exists(@$data['expire_time']) && $where['expire_time'] = ['<=', $data['expire_time']];
        is_real_exists(@$data['create_time']) && $where['create_time'] = ['between', $data['create_time']];
        is_real_exists(@$data['status']) && $where['status'] = $data['status'];

        $field = ['*', 'condition as condition_cn'];
        return $this->coupon->getListByPage($where, $field);
    }

}
